package cn.edu.sdjzu.xg23.kcsj01_backend.controller;


import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Patent;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.PatentService;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController()
@RequestMapping("/zlgl")
public class PatentController {
    @Autowired
    private PatentService patentService;
    @PreAuthorize("hasAuthority('patent:add')")
    @PostMapping("/patent")
    public Result addPatent(@RequestBody Patent patent) {
        if (patentService.addPatent(patent)) {
            return Result.success("增加成功");
        }else{
            return Result.error("500", "增加失败");
        }
    }

    @PreAuthorize("hasAuthority('patent:delete')")
    @DeleteMapping("/patent/{PatentId}")
    public Result deletePatent(@PathVariable Long PatentId) {
        if (patentService.deletePatent(PatentId)) {
            return Result.success("删除成功");
        } else {
            return Result.error("500", "删除失败");
        }
    }
    @PreAuthorize ("hasAuthority('patent:update')")
    @PutMapping("/patent")
    public Result updatePatent(@RequestBody Patent patent) {
        if (patentService.updatePatent(patent)) {
            return Result.success("更新成功");
        } else {
            return Result.error("500", "更新失败");
        }
    }

    // 模糊条件分页查询
    @PreAuthorize("hasAuthority('patent:search:all') or hasAuthority('patent:search:my')")
    @PostMapping("/search")
    public Result selectPatentsByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) Patent patent,
            @RequestParam(required = false) Long employeeId) {
        PageInfo<Patent> pageInfo = patentService.selectPatentsByCondition(pageNum, pageSize, patent,employeeId);
        return Result.success(pageInfo);
    }
    //专利成果转化
    @PreAuthorize( "hasAuthority('patent:transform')")
    @PutMapping("/transform")
    public Result transformPatent(@RequestBody Patent patent) {
        if (patentService.transformPatent(patent)) {
            return Result.success("转化成功");
        } else {
            return Result.error("500", "转化失败");
        }
    }

    @PreAuthorize("hasAuthority('patent:approve')")
    @PutMapping("/approve")
    public Result approvePatent(@RequestBody Patent patent) {
        if (patentService.approvePatent(patent)) {
            return Result.success("审批成功");
        } else {
            return Result.error("500", "审批失败");
        }
    }



}